package com.movile.checkhoras.android;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.movile.checkhoras.android.dao.TableCheckHorasHelper;
import com.movile.checkhoras.android.vo.CheckHora;
import com.movile.checkhoras.android.widget.CheckWidget1Provider;
import com.movile.checkhoras.android.widget.UpdateWidgetService;

public class CheckHorasActivity extends Activity {
    
    TableCheckHorasHelper tableCheckHoras;
    
    ImageButton btCheck;
    
    ImageButton btListaMeses;
    
    ImageButton btManual;
    
    ImageButton btSobre;
    
    TextView txtCheck;
    
    Activity instance;
    
    
    @Override
    protected void onResume() {
        updateCheck();
        super.onResume();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkauto);
        
        instance = this;
        
        btCheck = (ImageButton) findViewById(R.id.btCheck);
        btListaMeses = (ImageButton) findViewById(R.id.btListar);
        btManual = (ImageButton) findViewById(R.id.btManual);
        txtCheck = (TextView) findViewById(R.id.txtCheck);
        btSobre = (ImageButton) findViewById(R.id.btSobre);
        
        tableCheckHoras = new TableCheckHorasHelper(getBaseContext());
        
        updateCheck();
        
        btCheck.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                

                Calendar cal = Calendar.getInstance();
                cal.roll(Calendar.DATE, false);                
                int mes = cal.get(Calendar.MONTH) + 1;
                int dia = cal.get(Calendar.DAY_OF_MONTH) + 1;

                 CheckHora check = new CheckHora();            
                 
                 check.setAno(cal.get(Calendar.YEAR));
                 check.setDataString(cal.get(Calendar.YEAR) + "-" + mes + "-" + dia + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));
                 check.setDia(cal.get(Calendar.DAY_OF_MONTH) + 1);
                 check.setHora(cal.get(Calendar.HOUR_OF_DAY));
                 check.setMinuto(cal.get(Calendar.MINUTE));
                 check.setMes(mes);
                 check.setMilisegundos(cal.getTime().getTime());
                                 
                 
                 if(txtCheck.getText().toString().equals("CheckIn")){
                     check.setCheckIn(true);
                 } else {
                     check.setCheckIn(false);
                 }
                 
                 tableCheckHoras.insert(check);
                 
                 ComponentName thisWidget = new ComponentName(getBaseContext(), CheckWidget1Provider.class);
                 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
                 int[] idsWids = appWidgetManager.getAppWidgetIds(thisWidget);
                 
                 final int N = idsWids.length;
                 
                 
                 
                 

                 // Perform this loop procedure for each App Widget that belongs to this provider
                 for (int i = 0; i < N; i++) {
                     int appWidgetId = idsWids[i];                        
                     
                     RemoteViews views = new RemoteViews(getBaseContext().getPackageName(), R.layout.widget_1_layout);
                     
                     updateCheck(views);
                     
                     
                     
                     
                     
                     Intent intent = new Intent(getBaseContext(),UpdateWidgetService.class);
                     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,idsWids[i]);            
                     PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), appWidgetId, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
                     
                     views.setOnClickPendingIntent(R.id.btWidgetCheck, pendingIntent);
                     
                                                                  
                     appWidgetManager.updateAppWidget(appWidgetId, views);
                 }
                 
                 updateCheck();
                 
            }
        });
        
        btListaMeses.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                Intent it = new Intent(getBaseContext(), ListarMesAnoActivity.class);
                startActivity(it);
                
            }
        });
        
        btManual.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
           
                Intent it = new Intent(getBaseContext(), CheckManualActivity.class);
                startActivity(it);
                
            }
        });
        
        btSobre.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                Dialog dialog = new Dialog(instance);                
                dialog.setCancelable(true);
                dialog.setContentView(R.layout.dialogsobre);
                dialog.show();
                    
                
                
                
            }
        });
        
    }
    
    private void updateCheck(){
        if(tableCheckHoras.IsCheckInLast()){
            txtCheck.setText("CheckOut");
            btCheck.setImageResource(R.drawable.checkout);
        } else {
            txtCheck.setText("CheckIn");
            btCheck.setImageResource(R.drawable.checkin);           
        }
    }
    
    private void updateCheck(RemoteViews views){
        if(tableCheckHoras.IsCheckInLast()){
            views.setTextViewText(R.id.txtWidgetCheck, "CheckOut");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkoutwid);
        } else {
            views.setTextViewText(R.id.txtWidgetCheck, "CheckIn");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkinwid);           
        }
    }
}